from common.unit import AndroidUI
from common.home_page import HomePageObject
from common.qr_code import QRCodeX
from object.lnitial.lnitial_page import LnitialObject
from object.storage.packing_boxes import PackingObject
from object.storage.storage_page import StorageObject
from common.request_result import GetCode
import time


# 科室装袋-固定箱码-装箱操作
class PackingBoxedTest:
    def __init__(self):
        self.unit = AndroidUI
        self.home = HomePageObject()
        self.qrcode = QRCodeX()
        self.ln = LnitialObject()
        self.get_code = GetCode()
        self.driver = AndroidUI.get_webdriver()
        self.pack = PackingObject()

    # 返回
    def back_button(self):
        self.driver.keyevent(4)

    # 判断是否登录，未登录就扫清理人二维码登录，已登录的情况下，不进行操作
    def check_login(self):
        # self.unit.scan(self.qrcode.zhaoqingli)
        try:
            self.ln.system_button()
            self.unit.scan(self.qrcode.zhaoqingli)
            print("登录成功")
        except:
            print("已经登录")

    # #通过点击进入装箱界面
    # def enter_box(self):
    #     try:
    #         self.home.enter_button().click()
    #         self.

    # 判断是否在装箱界面，如果是，就不操作，如果不是就扫垃圾袋二维码进入装箱界面
    def check_in_storage(self):
        try:
            self.pack.listbox_button()
            print("已经在装箱界面了")
        except:
            try:
                # 扫码最新的一条数据
                self.unit.scan(self.get_code.get_bag_cod(0))  # 0是第一袋垃圾袋
                self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
                print("通过扫码最新一袋垃圾袋进入装箱界面成功")
                AndroidUI().write_result("通过扫码最新一袋垃圾袋进入装箱界面", "成功")
            except:
                # self.unit.scan(self.get_code.get_bag_cod(0))
                AndroidUI.capture()
                print("通过扫码最新一袋垃圾袋进入装箱界面失败")
                AndroidUI().write_result("通过扫码最新一袋垃圾袋进入装箱界面", "失败")

    # 装箱操作，在装箱列表已经有垃圾的情况。进行装箱操作
    def pack_box_first(self):
        try:
            self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
            self.pack.pack_box_button().click()
            print('点击了装箱按钮')
            self.unit.scan(self.qrcode.box_id(1))  # 扫固定箱码n
            try:
                self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
                print('装箱界面在已有垃圾袋的情况下，装箱失败')
                AndroidUI.capture()
                AndroidUI().write_result("装箱界面在已有垃圾袋的情况下装箱", "失败")
            except:
                print(f'装箱界面在已有垃圾袋的情况下，装箱成功,第1个固定箱码')
                AndroidUI().write_result(f"装箱界面在已有垃圾袋的情况下装箱箱码为1", "成功")
        except:
            print("装箱界面的列表中没有垃圾袋")

    # 扫描从上往下排第n+1条垃圾袋的二维码
    def scan_bags(self, n):
        n2 = n + 1
        try:
            self.unit.scan(self.get_code.get_bag_cod(n))
            self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
            print(f"扫码垃圾袋{n2}成功")
            AndroidUI().write_result(f"扫码垃圾袋{n2}", "成功")
        except:
            print(f"扫码垃圾袋{n2}失败")
            AndroidUI.capture()
            AndroidUI().write_result(f"扫码垃圾袋{n2}", "失败")

    # 扫描第n+1固定箱码的二维码
    # def scan_boxes(self, n):
    #     n2 = n + 1
    #     try:
    #         self.unit.scan(self.qrcode.box_id(n))  # 扫固定箱码n
    #         try:
    #             self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
    #             print(f"扫码固定箱码{n}失败")
    #             AndroidUI.capture()
    #             AndroidUI().write_result(f"扫码固定箱码{n}", "失败")
    #         except:
    #             print(f"扫码固定箱码{n}成功")
    #             AndroidUI().write_result(f"扫码固定箱码{n}", "成功")
    #     except:
    #         print(f'扫固定箱码{n}失败')
    #         AndroidUI.capture()
    #         AndroidUI().write_result(f"扫码固定箱码{n}", "失败")

    # 扫箱吗就不断言了
    def scan_boxes(self, n):
        n2 = n + 1
        self.unit.scan(self.qrcode.box_id(n))  # 扫固定箱码n
        # try:
        #
        #     try:
        #         self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
        #         print(f"扫码固定箱码{n}失败")
        #         AndroidUI.capture()
        #         AndroidUI().write_result(f"扫码固定箱码{n}", "失败")
        #     except:
        #         print(f"扫码固定箱码{n}成功")
        #         AndroidUI().write_result(f"扫码固定箱码{n}", "成功")
        # except:
        #     print(f'扫固定箱码{n}失败')
        #     AndroidUI.capture()
        #     AndroidUI().write_result(f"扫码固定箱码{n}", "失败")

    # # 进行装箱操作，固定箱码  ，此处需要优化，速度过慢，断言也需优化
    # def pack_box_second1(self, x, y, n):
    #
    #
    #     for i in range(x, y, n):
    #         i2 = i + 1
    #         try:
    #             self.scan_bags(i)  # 扫码
    #             self.pack.pack_box_button().click()  # 点击装箱
    #             self.scan_boxes(i2)  # 扫固定箱码
    #
    #             try:
    #                 self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("NO")')
    #                 print(self.pack.pack_box_button().is_enabled())
    #                 print(f'扫描垃圾袋{i2}，并装箱失败')
    #                 AndroidUI.capture()
    #                 AndroidUI().write_result(f"扫描垃圾袋{i2}并装箱{i2}", "失败")
    #             except:
    #                 print(f'扫描垃圾袋{i2}并装箱{i2}，装箱成功')
    #                 AndroidUI().write_result(f"扫描垃圾袋并装箱{i2}", "成功")
    #         except:
    #             print("扫码装箱失败")
    #             AndroidUI.capture()
    #             AndroidUI().write_result(f"扫描垃圾袋{i2}并装箱{i2}", "失败")

    # 进行装箱操作，固定箱码  ，此处需要优化，速度过慢，断言也需优化(已经通过if来判断装箱按钮的enable来缩短时间)
    def pack_box_second(self, x, y, n):
        for i in range(x, y, n):
            i2 = i + 1
            try:
                self.scan_bags(i)  # 扫码
                self.pack.pack_box_button().click()  # 点击装箱
                self.scan_boxes(i2)  # 扫固定箱码
                time.sleep(1)
                if self.pack.pack_box_button().is_enabled() is False:  # 通过装箱按钮的enable属性判断，False为置灰
                    print(f'扫描垃圾袋{i2}并装箱{i2}，装箱成功')
                    AndroidUI().write_result(f"扫描垃圾袋并装箱{i2}", "成功")
                else:
                    print(self.pack.pack_box_button().is_enabled())
                    print(f'扫描垃圾袋{i2}，并装箱失败')
                    AndroidUI.capture()
                    AndroidUI().write_result(f"扫描垃圾袋{i2}并装箱{i2}", "失败")
            except:
                print("扫码装箱失败")
                AndroidUI.capture()
                AndroidUI().write_result(f"扫描垃圾袋{i2}并装箱{i2}", "失败")

    # 扫描固定箱码
    def scan_fixed_box(self):
        self.unit.scan(self.qrcode.box_id(1))
        print("扫描固定箱码")

    def start_test(self, begin, end, n):
        # if box in "开启固定箱码":  # 此处得完善,box_first和box_second的代码,因为原来的点击装箱后就断言，需要另写方法或者优化方法
        #     print('开启固定箱码模式')
        #     self.check_login()
        #     # self.check_in_storage()
        #     self.pack_box_first()
        #     self.pack_box_second(begin, end)
        #     self.scan_fixed_box()  # 扫固定箱码
        # elif box in "关闭固定箱码":还有 个bug未解决（处置平台1个，）
        #     print('关闭固定箱码')
        self.check_login()
        self.check_in_storage()
        self.pack_box_first()
        self.pack_box_second(begin, end, n)
        self.back_button()


if __name__ == '__main__':
    test = PackingBoxedTest()
    test.start_test(1, 2, 1)
